/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JanelaPrincipal.java
 *
 * Created on 03/04/2011, 17:48:10
 */
package gui;

import javax.swing.JOptionPane;

/**
 *
 * @author Julio
 */
public class JanelaPrincipal extends javax.swing.JFrame {

    /** Creates new form JanelaPrincipal */
    public JanelaPrincipal() {
        initComponents();
        cadastro = new CadastroControlador(txtListagem);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        grupoSexo = new javax.swing.ButtonGroup();
        lblNome = new javax.swing.JLabel();
        txtNome = new javax.swing.JTextField();
        lblSexo = new javax.swing.JLabel();
        rdFeminino = new javax.swing.JRadioButton();
        rdMasculino = new javax.swing.JRadioButton();
        btAdicionar = new javax.swing.JButton();
        painelListagem = new javax.swing.JScrollPane();
        txtListagem = new javax.swing.JTextArea();
        barraMenu = new javax.swing.JMenuBar();
        menuListagens = new javax.swing.JMenu();
        itemHomens = new javax.swing.JMenuItem();
        itemMulheres = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Cadastro de Pessoas");

        lblNome.setText("Nome:");

        lblSexo.setText("Sexo:");

        grupoSexo.add(rdFeminino);
        rdFeminino.setSelected(true);
        rdFeminino.setText("F");

        grupoSexo.add(rdMasculino);
        rdMasculino.setText("M");

        btAdicionar.setText("Adicionar");
        btAdicionar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAdicionarActionPerformed(evt);
            }
        });

        txtListagem.setColumns(20);
        txtListagem.setEditable(false);
        txtListagem.setRows(5);
        painelListagem.setViewportView(txtListagem);

        menuListagens.setText("Listagens");

        itemHomens.setText("Homens");
        itemHomens.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                itemHomensActionPerformed(evt);
            }
        });
        menuListagens.add(itemHomens);

        itemMulheres.setText("Mulheres");
        itemMulheres.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                itemMulheresActionPerformed(evt);
            }
        });
        menuListagens.add(itemMulheres);

        barraMenu.add(menuListagens);

        setJMenuBar(barraMenu);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(painelListagem, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblNome)
                            .addComponent(lblSexo))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(rdFeminino)
                                .addGap(18, 18, 18)
                                .addComponent(rdMasculino)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 180, Short.MAX_VALUE)
                                .addComponent(btAdicionar))
                            .addComponent(txtNome, javax.swing.GroupLayout.DEFAULT_SIZE, 339, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblNome)
                    .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblSexo)
                    .addComponent(rdFeminino)
                    .addComponent(rdMasculino)
                    .addComponent(btAdicionar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(painelListagem, javax.swing.GroupLayout.DEFAULT_SIZE, 185, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btAdicionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAdicionarActionPerformed
        String nome = txtNome.getText();
        if (cadastro.adicionarPessoa(nome, rdMasculino.isSelected())) {
            JOptionPane.showMessageDialog(this, "Pessoa adicionada com sucesso.");
            txtNome.setText("");
            rdFeminino.setSelected(true);
        }
        else {
            JOptionPane.showMessageDialog(this, "Falha na adição de pessoa");
        }
    }//GEN-LAST:event_btAdicionarActionPerformed

    private void itemHomensActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemHomensActionPerformed
        JanelaListagem janela = new JanelaListagem(cadastro.buscarHomens());
        janela.setVisible(true);
    }//GEN-LAST:event_itemHomensActionPerformed

    private void itemMulheresActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemMulheresActionPerformed
        JanelaListagem janela = new JanelaListagem(cadastro.buscarMulheres());
        janela.setVisible(true);
    }//GEN-LAST:event_itemMulheresActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new JanelaPrincipal().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuBar barraMenu;
    private javax.swing.JButton btAdicionar;
    private javax.swing.ButtonGroup grupoSexo;
    private javax.swing.JMenuItem itemHomens;
    private javax.swing.JMenuItem itemMulheres;
    private javax.swing.JLabel lblNome;
    private javax.swing.JLabel lblSexo;
    private javax.swing.JMenu menuListagens;
    private javax.swing.JScrollPane painelListagem;
    private javax.swing.JRadioButton rdFeminino;
    private javax.swing.JRadioButton rdMasculino;
    private javax.swing.JTextArea txtListagem;
    private javax.swing.JTextField txtNome;
    // End of variables declaration//GEN-END:variables
    private CadastroControlador cadastro;
}
